package com.example.booksystem.view.page;

import com.example.booksystem.controller.BookController;
import com.example.booksystem.model.Book;
import com.example.booksystem.view.Index;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.SQLException;

public class InsertBook {
    public static void show(){
        Stage stage = new Stage();
        VBox vBox = new VBox();
        String[] newBook = {"id:","请输入13位书籍ISBN码","书名:","请输入书名","作者:","请输入作者","出版社:","请输入出版社","价格:","请输入价格","数量","请输入书籍数量"};
        TextField[] textFields = new TextField[6];
        for (int i = 0; i < 6; i++) {
            Label label = new Label(newBook[i*2]);
            textFields[i] = new TextField();
            textFields[i].setPromptText(newBook[i*2+1]);
            HBox box = new HBox();
            box.setAlignment(Pos.CENTER);
            box.getChildren().add(label);
            box.getChildren().add(textFields[i]);
            vBox.getChildren().add(box);
        }
        HBox box = new HBox();
        box.setAlignment(Pos.CENTER);
        Button update = new Button("添加");
        box.getChildren().add(update);
        vBox.getChildren().add(box);
        update.setOnAction(e->{
            Book book = new Book();
            book.setId(textFields[0].getText());
            book.setTitle(textFields[1].getText());
            book.setAuthor(textFields[2].getText());
            book.setPress(textFields[3].getText());
            book.setPrice(textFields[4].getText());
            book.setNum(Integer.valueOf(textFields[5].getText()));
            try {
                if (new BookController().insert(book)){
                    Index.success("添加成功");
                }
                stage.close();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        });
        Scene scene = new Scene(vBox,300,300);
        stage.setTitle("添加图书");
        stage.setScene(scene);
        stage.show();
    }
}
